package cn.mastor.pet.service.impl;

import cn.mastor.basic.util.BaiduAuditUtils;
import cn.mastor.basic.util.FreemarkUtil;
import cn.mastor.org.domain.Department;
import cn.mastor.pet.domain.Pet;
import cn.mastor.pet.domain.PetDetail;
import cn.mastor.pet.domain.PetOnlineAuditLog;
import cn.mastor.pet.domain.PetType;
import cn.mastor.pet.mapper.PetDetailMapper;
import cn.mastor.pet.mapper.PetMapper;
import cn.mastor.pet.mapper.PetOnlineAuditLogMapper;
import cn.mastor.pet.mapper.PetTypeMapper;
import cn.mastor.pet.service.IPetDetailService;
import cn.mastor.pet.service.IPetService;
import cn.mastor.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.connection.ConnectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-10-28
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Value("${pagedir}")
    private String path;
    @Autowired
    PetTypeMapper petTypeMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    @Autowired
    private IPetDetailService petDetailService;
    @Autowired
    PetDetailMapper petDetailMapper;

    @Autowired
    PetMapper petMapper;
    @Override
    public void add(Pet pet) {
        super.add(pet);
        if (null!=pet.getDetail()) {
            pet.getDetail().setPetId(pet.getId());
            petDetailService.add(pet.getDetail());
        }
    }

    @Override
    public List<PetType> PetTypeTree() {
        List<PetType> all = petTypeMapper.findAll();
        List<PetType> result = new ArrayList<>();
        Map<Long,PetType> parentMap = all.stream().collect(Collectors.toMap(PetType::getId, e->e));
        System.out.println(parentMap);
        for (PetType petType:all) {
            if (null == petType.getPid()) {
                // 它就是最大的那个die
                result.add(petType);
            } else {
                // 当前的这个petType 是个儿子
                // 找爹 自己是个儿子 有爹的id
                PetType parent = parentMap.get(petType.getPid());
                List<PetType> children = parent.getChildren();
                children.add(petType);
            }
        }
        return result;
    }

    @Override
    public PetDetail findDetail(Long id) {
        PetDetail petDetail = petDetailMapper.findDetail(id);
        return petDetail;
    }

    @Override
    public void onsalePet(List<Long> ids) {
        System.out.println("---------"+path);

//        Map<Long, Pet> petMaps = petMapper.findAll().stream().filter(pet -> pet.getState() == 0).collect(Collectors.toMap(Pet::getId, pet -> pet));
        Date date = new Date();
        Map<Long, Pet> petMaps = new TreeMap<>();
        for (int i = 0; i < ids.size(); i++) {
            Pet byId = petMapper.findById(ids.get(i));
            if (byId!=null){
                petMaps.put(byId.getId(), byId);
            }
        }

        for (Long id:ids){
            Pet pet = petMaps.get(id);
            StringBuilder sb = new StringBuilder();
            if (pet != null){
                System.out.println(pet.getResources());
                if (!BaiduAuditUtils.TextCensor(pet.getName())){
                    sb.append("宠物上架百度AI审核名称失败，宠物名为："+pet.getResources());
                }
                if (pet.getResources().split(",").length > 1){
                    if (!BaiduAuditUtils.ImgCensor(pet.getResources().split(",")[0])){
                        sb.append("宠物上架百度AI审核图片失败，宠物图片地址为：" + pet.getResources() + ";");
                    }
                }else {
                    if (!BaiduAuditUtils.ImgCensor(pet.getResources())){
                        sb.append("宠物上架百度AI审核图片失败，宠物图片地址为：" + pet.getResources() + ";");
                    }
                }
                if (sb.length()>0){
                    PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                    petOnlineAuditLog.setAuditTime(date);
                    petOnlineAuditLog.setPetId(id);
                    petOnlineAuditLog.setNote(sb.toString());
                    petOnlineAuditLog.setState(0);
                    petOnlineAuditLogMapper.add(petOnlineAuditLog);
                    continue;
                }
                pet.setState(1L);
                pet.setOnsaletime(date);
                petMapper.update(pet);
                try {
                    FreemarkUtil.testHtml(id,path);
                    System.out.println("-----------哈哈"+path);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Override
    public void offsale(List<Long> ids) {
        // 如果要判断，全部查询出来，只要状态是下架那么就不处理
        for (int i = 0; i < ids.size(); i++) {
            File file = new File("C:/Users/dscc/Desktop/bi前端2/pethome-web/department-web/detail/" + ids.get(i) + ".html");
            file.delete();
        }
        petMapper.batchOffsale(ids);
    }

    @Override
    public Pet getDetail(Long id) {
        return petMapper.getDetail(id);
    }


}
